<template>
  <div class="stat-card" :class="{ 'is-loading': loading }">
    <div class="stat-icon" :style="{ backgroundColor: iconBgColor }">
      <div v-if="icon" class="icon" v-html="icon" />
      <span v-else-if="iconText" class="icon-text">{{ iconText }}</span>
    </div>
    <div class="stat-content">
      <div class="stat-label">{{ label }}</div>
      <div class="stat-value">{{ formattedValue }}</div>
      <div v-if="trend !== null" class="stat-trend" :class="trendClass">
        <span class="trend-icon" v-html="trendIcon" />
        <span class="trend-text">{{ trendText }}</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import type { Component } from 'vue'

interface Props {
  label: string
  value: string | number
  icon?: Component | string
  iconText?: string
  iconBgColor?: string
  loading?: boolean
  trend?: number // 变化趋势，正数为增长，负数为下降
  trendText?: string
  formatter?: (value: string | number) => string
}

const props = withDefaults(defineProps<Props>(), {
  iconBgColor: '#2563eb',
  loading: false,
  trend: undefined,
  trendText: ''
})

const formattedValue = computed(() => {
  if (props.formatter) {
    return props.formatter(props.value)
  }
  return String(props.value)
})

const trendClass = computed(() => ({
  'trend-up': props.trend !== undefined && props.trend > 0,
  'trend-down': props.trend !== undefined && props.trend < 0,
  'trend-neutral': props.trend === 0
}))

const trendIcon = computed(() => {
  if (props.trend === undefined) return '→'
  if (props.trend > 0) return '↗'
  if (props.trend < 0) return '↘'
  return '→'
})
</script>

<style scoped>
.stat-card {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 24px;
  background: var(--card-bg);
  border-radius: 12px;
  border: 1px solid var(--border);
  transition: all 0.2s ease;
  position: relative;
  overflow: hidden;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}

.stat-card.is-loading {
  pointer-events: none;
}

.stat-card.is-loading::after {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
  animation: loading 1.5s infinite;
}

.stat-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 20px;
  font-weight: 600;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
}

.icon {
  width: 24px;
  height: 24px;
}

.icon-text {
  font-size: 18px;
}

.stat-content {
  flex: 1;
  min-width: 0;
}

.stat-label {
  font-size: 14px;
  color: var(--text-muted);
  font-weight: 500;
  margin-bottom: 4px;
}

.stat-value {
  font-size: 24px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
}

.stat-trend {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 8px;
  font-size: 12px;
  font-weight: 500;
}

.trend-up {
  color: var(--success);
}

.trend-down {
  color: var(--danger);
}

.trend-neutral {
  color: var(--text-muted);
}

.trend-icon {
  font-size: 12px;
}

.trend-text {
  font-size: 12px;
}

@keyframes loading {
  0% { left: -100%; }
  100% { left: 100%; }
}

/* 暗色模式支持 */
@media (prefers-color-scheme: dark) {
  .stat-card.is-loading::after {
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.1), transparent);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .stat-card {
    padding: 20px;
    gap: 12px;
  }
  
  .stat-icon {
    width: 40px;
    height: 40px;
    font-size: 16px;
  }
  
  .icon {
    width: 20px;
    height: 20px;
  }
  
  .icon-text {
    font-size: 14px;
  }
  
  .stat-label {
    font-size: 12px;
  }
  
  .stat-value {
    font-size: 20px;
  }
}
</style>